<?php

namespace lxs\tools;

use Illuminate\Support\Facades\Redis;
use lxs\config\BaseStatic;

class CacheRedis extends BaseStatic
{

    /**
     * 限流
     * @param $key
     * @param $limit
     * @param int $expire
     * @return bool
     */
    public static function limit($key, $limit, int $expire = 30): bool
    {
        $count = Redis::get($key);
        if ($count) {
            if ($count >= $limit) {
                return false;
            }
            $ttl = Redis::ttl($key);
            Redis::setex($key, $ttl, (int)$count + 1);
        } else {
            Redis::setex($key, $expire, 1);
        }
        return true;
    }

}
